package com.example.cxx.controller;

import com.example.cxx.pojo.Result;
import com.example.cxx.pojo.YuMing;
import com.example.cxx.server.LoginServerce;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("course/api/*")
public class LoginController {
    @Autowired
    private LoginServerce loginServerce;
    @GetMapping("/login")
    public Result login(String userName,String password) throws IOException {
        Connection connection=Jsoup.connect(YuMing.qianzhui+YuMing.yuming+"/EaWeb/Manager/Login.aspx");
        Document d=connection.method(Connection.Method.GET).get();
        //获取仿登录的数据
        String viewStates=d.getElementById("__VIEWSTATE").val();
        String viewStatesGenerator=d.getElementById("__VIEWSTATEGENERATOR").val();
        //用map来存储数据
        Map map=new HashMap();
        map.put("__VIEWSTATE",viewStates);
        map.put("__VIEWSTATEGENERATOR",viewStatesGenerator);
        map.put("Account",userName);
        map.put("LoginPass",password);
        map.put("ButtonOK","确定");
        try {
            //登录，获取cookies
            Connection.Response login=Jsoup.connect(YuMing.qianzhui+YuMing.yuming+"/EaWeb/Manager/Login.aspx")
                    .ignoreContentType(true)
                    .followRedirects(false)
                    .data(map)
                    .method(Connection.Method.POST)
                    .execute()
                    .charset("utf-8");
         //   System.out.println(login.parse());
            String url=login.header("Location");
            String newUrl=YuMing.qianzhui+url.split("//")[1];
            Document messageNote=Jsoup.connect(newUrl).cookies(login.cookies()).method(Connection.Method.GET).get();
         //   System.out.println(messageNote.html());
            String info=messageNote.getElementsByClass("style3").text();
            if("登录成功".equals(info)){
//                try {
//                    if(loginServerce.insertUser(Integer.parseInt(userName),password)){
//                        loginServerce.insertDes(code);
//                    }
//                }
//                finally {
           //     System.out.println(login.cookies());
                    return Result.success(login.cookies());
      //          }
                //打印数据网页

            }else {
                return Result.error("LOGIN-FAIL");
            }
        }catch (Exception e){
            return Result.error("LOGIN-FAIL");
        }


    }
    public Result loginWithNoCode(String userName,String password) throws IOException {
        Connection connection=Jsoup.connect(YuMing.qianzhui+YuMing.yuming+"/EaWeb/Manager/Login.aspx");
        Document d=connection.method(Connection.Method.GET).get();

        //获取仿登录的数据
        String viewStates=d.getElementById("__VIEWSTATE").val();
        String viewStatesGenerator=d.getElementById("__VIEWSTATEGENERATOR").val();
        //用map来存储数据
        Map map=new HashMap();
        map.put("__VIEWSTATE",viewStates);
        map.put("__VIEWSTATEGENERATOR",viewStatesGenerator);
        map.put("Account",userName);
        map.put("LoginPass",password);
        map.put("ButtonOK","确定");
        try {
            //登录，获取cookies
            Connection.Response login=Jsoup.connect(YuMing.qianzhui+YuMing.yuming+"/EaWeb/Manager/Login.aspx")
                    .ignoreContentType(true)
                    .followRedirects(false)
                    .data(map)
                    .method(Connection.Method.POST)
                    .execute()
                    .charset("utf-8");
//            System.out.println(login.parse());
            String url=login.header("Location");
            String newUrl="https://"+url.split("//")[1];
            Document messageNote=Jsoup.connect(newUrl).cookies(login.cookies()).method(Connection.Method.GET).get();
            String info=messageNote.getElementsByClass("style3").text();
            if("登录成功".equals(info)){
                System.out.println(login.cookies());
                    return Result.success(login.cookies());
                //打印数据网页
            }else {
                return Result.error("LOGIN-FAIL");
            }
        }catch (Exception e){
            return Result.error("LOGIN-FAIL");
        }

    }
}
